package cn.com.duiba.millionaire.center.biz.service.impl;

import cn.com.duiba.millionaire.center.api.constant.RedisKeyFactory;
import cn.com.duiba.millionaire.center.api.dto.MillionaireConsumerDto;
import cn.com.duiba.millionaire.center.biz.dao.MillionaireConsumerDao;
import cn.com.duiba.millionaire.center.biz.dao.MillionaireShareCodeDao;
import cn.com.duiba.millionaire.center.biz.dao.MillionaireShareCodeUserRelationDao;
import cn.com.duiba.millionaire.center.biz.entity.MillionaireConsumerEntity;
import cn.com.duiba.millionaire.center.biz.entity.MillionaireShareCodeEntity;
import cn.com.duiba.millionaire.center.biz.entity.MillionaireShareCodeUserRelationEntity;
import cn.com.duiba.millionaire.center.biz.service.MillionaireConsumerService;
import cn.com.duiba.wolf.utils.BeanUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.concurrent.TimeUnit;

/**
 * Created by zzy on 2018/1/18.
 */
@Service
public class MillionaireConsumerServiceImpl implements MillionaireConsumerService {
    @Autowired
    private MillionaireConsumerDao millionaireConsumerDao;
    @Autowired
    private MillionaireShareCodeDao millionaireShareCodeDao;
    @Autowired
    private MillionaireShareCodeUserRelationDao millionaireShareCodeUserRelationDao;
    @Resource(name="redisTemplate")
    private RedisTemplate<String,MillionaireConsumerDto> redisTemplate;

    private String buildKey(Long consumerId){
        return RedisKeyFactory.K008.toString()+consumerId;
    }
    @Override
    public MillionaireConsumerDto findByConsumerId(Long consumerId, Long appId, String shareCode) {
        MillionaireConsumerDto consumerInRedis = redisTemplate.opsForValue().get(buildKey(consumerId));
        if(consumerInRedis != null){
            return consumerInRedis;
        }
        MillionaireConsumerDto consumerDto = getConsumerByConsumerId(consumerId, appId, shareCode);
        return consumerDto;
    }

    @Override
    public MillionaireConsumerDto findByConsumerIdNoCache(Long consumerId, Long appId, String shareCode) {
        return getConsumerByConsumerId(consumerId, appId, shareCode);
    }

    private MillionaireConsumerDto getConsumerByConsumerId(Long consumerId, Long appId, String shareCode){
        MillionaireConsumerEntity consumer =  millionaireConsumerDao.findByConsumerId(consumerId);
        if(consumer != null){
            if(StringUtils.isBlank(consumer.getShareCode())){
                if(StringUtils.isBlank(shareCode)) {
                    shareCode = createShareCodeByDuiba();
                }
                if (StringUtils.isBlank(shareCode)) {
                    return null;
                }
                int result = millionaireConsumerDao.updateShareCode(consumerId, shareCode);
                if(result > 0){
                    addRelationUserAndCode(consumerId, appId, shareCode);
                    consumer.setShareCode(shareCode);
                }
            }
            MillionaireConsumerDto consumerDto = BeanUtils.copy(consumer, MillionaireConsumerDto.class);
            redisTemplate.opsForValue().set(buildKey(consumerId), consumerDto, 1, TimeUnit.HOURS);
            return consumerDto;
        }
        consumer = new MillionaireConsumerEntity();
        consumer.setConsumerId(consumerId);
        consumer.setAppId(appId);
        consumer.setAccount(0L);
        consumer.setRenascenceNum(0);
        //获取新的分享码
        if(StringUtils.isBlank(shareCode)) {
            shareCode = createShareCodeByDuiba();
        }
        if (StringUtils.isBlank(shareCode)) {
            return null;
        }

        consumer.setShareCode(shareCode);

        MillionaireConsumerEntity newUser = millionaireConsumerDao.insert(consumer);
        if(newUser!=null){
            //添加邀请码和用户绑定关系
            addRelationUserAndCode(consumerId, appId, shareCode);
        }

        return BeanUtils.copy(newUser, MillionaireConsumerDto.class);
    }

    private void addRelationUserAndCode(Long consumerId, Long appId, String shareCode) {
        MillionaireShareCodeUserRelationEntity relation = new MillionaireShareCodeUserRelationEntity();
        relation.setConsumerId(consumerId);
        relation.setShareCode(shareCode);
        relation.setAppId(appId);
        millionaireShareCodeUserRelationDao.insert(relation);
    }

    private String createShareCodeByDuiba() {
        MillionaireShareCodeEntity shareCodeLong = millionaireShareCodeDao.insert(new MillionaireShareCodeEntity());
        if(shareCodeLong==null){
            return null;
        }
        String shareCode = Long.toString(shareCodeLong.getShareCode(), 36);
        int size  = shareCode.length();
        if(size < 6){
            StringBuilder builder = new StringBuilder();
            for(int i=0 ;i< 6-size ;i++) {
                builder.append("0");
            }
            builder.append(shareCode);
            shareCode = builder.toString();
        }
        return shareCode;
    }

    @Override
    public MillionaireConsumerDto select4Update(long consumerId) {
        return BeanUtils.copy(millionaireConsumerDao.select4Update(consumerId), MillionaireConsumerDto.class);
    }

    @Override
    public boolean withdrawCash(long consumerId, int changeAmount) {
        return millionaireConsumerDao.withdrawCash(consumerId, changeAmount);
    }

    @Override
    public boolean withdrawCashPayback(long consumerId, int changeAmount) {
        return millionaireConsumerDao.withdrawCashPayback(consumerId, changeAmount);
    }


}
